home *** CD-ROM | disk | FTP | other *** search
- Path: pcnet.com!usenet
- From: Ron Lawrence <lawrenc@pcnet.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Class variables in C++
- Date: Fri, 16 Feb 1996 18:08:53 -0500
- Organization: PCNet -- Public Access Internet in Connecticut!
- Message-ID: <31250E85.71FAA57B@pcnet.com>
- References: <1996Feb14.165316@eigb> <3123D4BF.7C9@aai.arco.com>
- NNTP-Posting-Host: ts3-pt2.pcnet.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.1 i586)
-
- Here are some options for creating "constants" in a class (in the order
- that they occurred to me):
-
- 1) For integer constants (only), include a (possibly anonymous)
- enumeration:
- class X
- {
- public:
- enum { INT_CONSTANT=123 };
- //or
- enum namedEnum { P, Q };
- };
- N.B. this is the only way to create constants for declaring sizes
- for embedded arrays, like:
- class A
- {
- public:
- // ...
- private:
- enum { BUFSIZE=1024 };
- char buffer[BUFSIZE];
- };
-
- 2) include a static const variable in the class declaration. Define
- the constant in a source file somewhere.
- class Y
- {
- public:
- // ...
- private:
- static const double DBL_CONSTANT;
- };
-
- // in the .cc file:
- const double Y::DBL_CONSTANT=3.14159;
-
- 3) include a constant member data element in the class declaration.
- This constant data member must be initialized as one of the initializers
- in each of the constructors defined for the class. If
- class Z
- {
- public:
- Z();
- Z(double);
- private:
- const double DBL_CONSTANT;
- };
-
- // in the .cc file:
- Z::Z() : DBL_CONSTANT(3.14159)
- {}
- Z::Z(double z): DBL_CONSTANT(z)
- {}
-
- N.B. if you have a constructor that can be called which doesn't
- initialize the constant it will remain uninitialized forever in the
- constructed object. G++ 2.6.3 doesn't complain about this, I don't know
- if other compilers do.
-
- 4) Include member functions that return the constant. This technique
- is described below in Brian Leach's followup posting.
-
- 5) You can also use the preprocessor to create constants, but this
- is deprecated in C++. e.g.
- #define DBL_CONSTANT 3.14159
-
- These are the techniques that I am aware of. Are there any others?
- If anyone knows of any, I'd like to know more about them.
-
- I hope this helps out.
-
-
- Original postings follow:
- Brian Leach wrote:
- >
- > guinnard@eigb wrote:
- > >
- > > Hi,
- > >
- > > Is it possible to create a class variable (or constant) in C++ ?
- > >
- > > For example:
- > >
- > > class ANIMAL
- > > {
- > > protected:
- > > <I would like a class-variable (or constant), please>
- > > UINT uNumberOfLeg=0; /* pure ?!?! */
- > >
- > > public:
- > > ...
- > > };
- > >
- > > class CAT : public ANIMAL
- > > {
- > > protected:
- > > <I would like a class-variable (or constant), please>
- > > UINT uNumberOfLeg=4;
- > >
- > > public:
- > > ...
- > > };
- > >
- > > Because I have never see a cat with 5 legs...
- > > (I don't want to reserve 2 or more bytes in each instance of this class to
- > > save the number of leg for a cat...)
- > >
- > > I hope this is comprehensive...
- > >
- > > Thanks
- > >
- > > Laurent Guinnard
- > > guinnard@eig.unige.ch
- > >
- > > be_well++;
- >
- > class Animal {
- > public:
- > size_t numberOfLegs() const =0;
- > };
- >
- > class Cat: public Animal {
- > public:
- > size_t numberOfLegs() const { return 4; }
- > };
- >
- > class Spider: public Animal {
- > public:
- > size_t numberOfLegs() const { return 8; }
- > };
- >
- > There are other ways to do it, but this is the most straight-forward
- > I could come up with.
- >
- > -Brian :-)
-